package cn.wolfcode.p2p.business.service.impl;

import java.util.Date;
import java.util.List;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.BaseAuditDomain;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.mapper.RechargeOfflineMapper;
import cn.wolfcode.p2p.business.query.RechargeQueryObject;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import cn.wolfcode.p2p.util.Assert;
import cn.wolfcode.p2p.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class RechargeOfflineServiceImpl implements IRechargeOfflineService {

    @Autowired
    private RechargeOfflineMapper offlineMapper;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountFlowService accountFlowService;

    @Override
    public void apply(RechargeOffline rechargeOffline) {
        //参数校验

        //判断流水号是否已经被使用,通过查询数据库
        int count = offlineMapper.getCountByTradeCodeAndState(rechargeOffline.getTradeCode(), BaseAuditDomain.STATE_SUCCESS);

        Assert.isFalse(count > 0, "流水号已存在");

        //保存参数, new 对象
        RechargeOffline chargeInfo = new RechargeOffline();
        chargeInfo.setTradeCode(rechargeOffline.getTradeCode());
        chargeInfo.setTradeTime(rechargeOffline.getTradeTime());
        chargeInfo.setAmount(rechargeOffline.getAmount());
        chargeInfo.setNote(rechargeOffline.getNote());
        chargeInfo.setBankInfo(rechargeOffline.getBankInfo());
        chargeInfo.setApplyTime(new Date());
        chargeInfo.setApplier(UserContext.getLoginInfo());

        offlineMapper.insert(chargeInfo);


    }

    @Override
    public PageInfo selectForPage(RechargeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List list = offlineMapper.selectForPage(qo);
        return new PageInfo(list);
    }

    @Override
    public void auditRecharge(Long id, Integer state, String remark) {
        //校验参数
        //获取实体对象
        RechargeOffline rechargeOffline = offlineMapper.selectByPrimaryKey(id);
        //状态是否待审核
        Assert.isFalse(rechargeOffline.getState() != RechargeOffline.STATE_NORMAL, "当前实名状态不属于待审核状态");
        //是否存在相同的流水号
        //设置审核的参数
        //审核人员
        rechargeOffline.setAuditor(UserContext.getLoginInfo());
        //审核时间
        rechargeOffline.setAuditTime(new Date());
        //统一设置审核状态
        rechargeOffline.setState(state);
        //审核说明
        rechargeOffline.setRemark(remark);

        update(rechargeOffline);

        //如果审核成功
        if (state == RechargeOffline.STATE_SUCCESS) {
            //充值人的账户可用余额增加,
            Account account = accountService.getById(rechargeOffline.getApplier().getId());
            //抽取方法在实体类处理
            account.addUsableAmount(rechargeOffline.getAmount());
           // account.setUsableAmount(account.getUsableAmount().add(rechargeOffline.getAmount()));
            accountService.update(account);

            //生成充值流水, 参数:账户,充值金额
            accountFlowService.createAccountFlow(account,rechargeOffline.getAmount());
        }

    }

    //更新账户信息
    private void update(RechargeOffline rechargeOffline) {
        offlineMapper.updateByPrimaryKey(rechargeOffline);
    }
}
